$(function() {
	
	userRankValidate();
	
if($("#popuperrorMessage").val()==='true'){
	$('#userrank_add_and_update').modal('show');
}

$("#rankIstype").change(function(){
	$("#submitBuyFull").prop("disabled",false);
	var type=$(this).val();
	if(!isEmpty(type) && type=='1'){
		$("#rankDiscountDiv").attr("class","form-group show");
		$("#rankBuyfullIdDiv").attr("class","form-group hidden");
		$("#startAndEnd").attr("class","form-group show");
	}else if(!isEmpty(type) && type=='2'){
		$("#rankDiscountDiv").attr("class","form-group hidden");
		$("#rankBuyfullIdDiv").attr("class","form-group show");
		$("#startAndEnd").attr("class","form-group hidden");
		if($("#rankBuyfullIdDiv").attr("name")=='addbuyfulls'){
			$("#submitBuyFull").prop("disabled",true);
		}
	}else{
		$("#rankDiscountDiv").attr("class","form-group hidden");
		$("#rankBuyfullIdDiv").attr("class","form-group hidden");
		$("#startAndEnd").attr("class","form-group hidden");
	}
});

$("[name='cleanAll']").click(function(){
	if($(this).attr("type")!="reset"){
		$("#rankBuyfullId").val("");
	}
	$("#rankDiscountDiv").attr("class","form-group hidden");
	$("#rankBuyfullIdDiv").attr("class","form-group hidden");
	$("#startAndEnd").attr("class","form-group hidden");
	$(":text").val("");
	$("#rankAmtNum").val("");
	$("#rankIstype").find("option:selected").first().prop("selected", true);
	$("span[name='message']").text("");
	$("#errorMessageAll").remove();
	//Modal验证销毁重构
    $("#formUserRank").data('bootstrapValidator').destroy();
    $('#formUserRank').data('bootstrapValidator', null);
    userRankValidate();
});

$("b").each(function(){
	if(isEmpty($(this).text())){
		$(this).text("------");
	}
})
$("body").on("click",'a[name="show"]',function(){
	
	
	
	if(!isEmpty($(this).attr("id"))){
		var data = {
					'rankId' : $(this).attr("id"),
					};
		var datas=ajaxUtil("/TblWxUserrank/getTblWxUserrank", data, false, "","");
		if(!isEmpty(datas)){
			$("#rankName").val(datas.data.rankName);
			$("#rankId").val(datas.data.rankId);
			$("#rankCode").val(datas.data.rankCode);
			$("#rankAmtNum").val(datas.data.rankAmtNum);
			$("#rankIstype").find("option:selected").attr("selected", false);
			$("#rankIstype option").each(function(){
				var id = $(this).val();
				if (parseInt(datas.data.rankIstype) == parseInt(id)) {
					$(this).attr("selected",true);
				}
			});
			if(datas.data.rankIstype==1){
				$("#rankDiscountDiv").attr("class","form-group show");
				$("#rankBuyfullIdDiv").attr("class","form-group hidden");
				$("#startAndEnd").attr("class","form-group show");
				$("#rankDiscount").val(datas.data.rankDiscount);
				$("#rankDiscntStatimeView").val(datas.data.rankDiscntStatimeView);
				$("#rankDiscntEndtimeView").val(datas.data.rankDiscntEndtimeView);
			}else if(datas.data.rankIstype==2){
				$("#rankDiscountDiv").attr("class","form-group hidden");
				$("#rankBuyfullIdDiv").attr("class","form-group show");
				$("#startAndEnd").attr("class","form-group hidden");
				$("#rankBuyfullId").find("option:selected").prop("selected", false);
				if(("#rankBuyfullId option").length!=0){
					$("#rankBuyfullId option").each(function(){
						var id = $(this).val();
						if (datas.data.buyFullTemp.buyfullTempId == id) {
							$(this).prop("selected",true);
						}
					});
				}
			}else {
				$("#rankDiscountDiv").attr("class","form-group hidden");
				$("#rankBuyfullIdDiv").attr("class","form-group hidden");
				$("#startAndEnd").attr("class","form-group hidden");
				$("#rankIstype").find("option:selected").first().prop("selected", true);
			}
			$('#userrank_add_and_update').modal('show');
		}else{
			$('#userrank_add_and_update').modal('hide');
		}
	}
});

$("body").on("click",'.example5-3-2', function(){
	var menuUuid=$("input[name='menuUuid']").val();
	var rankId=$(this).attr("id");
	var url='/TblWxUserrank/delete?rankId='+rankId+"&menuUuid="+menuUuid;
	deleteAlert(url);
});

});

function userRankValidate(){
	$("#formUserRank").bootstrapValidator({
	    message: 'This value is not valid',
	    feedbackIcons: {
	        valid: 'glyphicon glyphicon-ok',
	        invalid: 'glyphicon glyphicon-remove',
	        validating: 'glyphicon glyphicon-refresh'
	    },
	    fields: {
	   	 rankName: {
	        	validators: {
	            	notEmpty : {
						message : '会员名称不能为空'
					},
					stringLength : {
						min : 1,
						max : 10,
						message : '会员名称过长'
					}
	            }
	        },
	        rankBuyfullId :{
	             message :'请选择买满立减模板',
	             validators:{
	                 notEmpty:{
	                     message:'请选择买满立减模板'
	                 },
	                 callback: {
	                     message: '必须选择一个买满立减模板',
	                     callback: function(value, validator) {
	                         if (value == -1 || value=='') {
	                             return false;
	                         } else {
	                             return true;
	                         }

	                     }
	                 }
	             }
	         },
	         rankDiscount: {
	           	validators: {
		           		notEmpty : {
							message : '请输入有效折扣'
						},
						regexp : {
	         				regexp : /^((0\.[1-9]{1})|(([1-9]{1})(\.\d{1})?))$/,
	         				message : '请输入有效折扣'
	         			}
	               }
	          },
	          rankDiscntStatimeView: {
	       	   message: '开始时间验证失败',
	              trigger:"change", // 监听change动作
	          	validators: {
	              	notEmpty : {
						message : '开始时间不能为空'
					}
	          	}
	         },
	         rankDiscntEndtimeView: {
	       	  message: '开结束时间验证失败',
	             trigger:"change", // 监听change动作
	            	validators: {
	                	notEmpty : {
							message : '结束时间不能为空'
						}
	            	}
	          },
	         rankAmtNum: {
	           	validators: {
		           		notEmpty : {
							message : '请输入有效金额'
						},
	         			regexp : {
	         				regexp : /(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/,
	         				message : '请输入有效金额'
	         			}
	               }
	           },
	    },
	    submitHandler: function (validator, form, submitButton) {
	        alert("submit");
	    }
	});
}
